<?php
/*
验证登录情况

 */
class RPHP_Auths
{

    public $c              = null;
    public $_prefix        = '';
    protected $db          = null;
    protected $privs_cache = null;
    protected $privs_menu  = null;

    public function __construct($db, $c, $prefix)
    {

        $this->db      = $db;
        $this->c       = $c;
        $this->_prefix = $prefix;
    }

    //检查是否登录
    public function CheckSignin($goto = true, $privmenu = false)
    {

        //如果有COOKIE
        if (isset($_COOKIE[$this->c['cookie']['user']]) && $_COOKIE[$this->c['cookie']['user']] != "") {
            $data = unserialize(Rfc::decode($_COOKIE[$this->c['cookie']['user']]));
            if ($data['aid'] && $data['token']) {

                $token = md5($this->c['secrkey'] . $data['aid'] . $data['signin_time'] . $data['signin_ip']);

                if ($token == $data['token']) {

                    if ($privmenu) {
                        $data['menu'] = $this->PrivMenu($data['aid']);
                    }

                    return $data;
                }

            }
        }

        if ($goto) {
            self::GotoSignin();
        } else {
            return false;
        }

    }

    //验证权限
    public function CheckPriv($pv, $return = true, $menu = true)
    {

        $user = $this->CheckSignin(false);

        if ($user['token']) {

            if (!$this->privs_cache) {
                //检测是否在线
                $online = $this->db->sql("SELECT `id`,`aid` FROM {$this->_prefix}admin_online WHERE `aid`=" . intval($user['aid']) . " AND `token`='" . $user['token'] . "'");
                if ($online) {
                    //获得获取权限
                    $rs = $this->db->sql("SELECT `privs` FROM {$this->_prefix}admin_account WHERE `aid`=" . $online[0]['aid'] . " AND `stat`=1");
                    if ($rs) {
                        $this->db->sql("UPDATE {$this->_prefix}admin_online SET `lastact_time`=" . $_SERVER['REQUEST_TIME'] . " WHERE `id`=" . $online[0]['id'] . " AND `aid`=" . $online[0]['aid'], true);
                        $this->privs_cache = $rs[0]['privs'];
                        if ($menu) {
                            $user['menu'] = $this->PrivMenu($user['aid'], $rs[0]['privs']);
                        }

                    }

                } //end of online

            }

            //核实权限
            if ($this->privs_cache == "ALL") {
                return $user;
            } elseif ($this->privs_cache != "NON" && $this->privs_cache != "") {
                $priv = explode(",", $this->privs_cache);
                if (is_array($pv)) {
                    //如果是数组
                    $len = count($pv);
                    $ls  = 0;
                    foreach ($pv as $r) {
                        if (in_array($r, $priv)) {
                            $ls += 1;
                        }

                    }
                    if ($ls == $len) {
                        return $user;
                    }
                } elseif (in_array($pv, $priv)) {
                    return $user;
                }
            }
        }

        //如果前面都没有成功的话，转到无权限显示页面
        if (!$return) {
            return false;
        } else {
            $url = "/alert/nopriv?path=" . $_SERVER['REQUEST_URI'];
            header("location: " . $url);
            exit;
        }
    }

    //获取权限菜单
    public function PrivMenu($aid = 0, $userpriv = false)
    {
        $menu = array();

        if (!$userpriv && $aid > 0) {
            $sql      = "SELECT `privs` FROM {$this->_prefix}admin_account WHERE `aid`=" . $aid . " AND `stat`=1";
            $userpriv = $this->db->getone($sql);
        }

        if ($userpriv != "" && $userpriv != "ALL" && $userpriv != "NON") {
            $pvs = explode(",", $userpriv);
            foreach ($pvs as $r) {
                $np                     = explode("_", $r);
                $umodel[$np[0]][$np[1]] = $np[1];
            }
        }

        //处理当前网址
        $nowurl   = explode("?", $_SERVER['REQUEST_URI']);
        $nowmodel = explode("/", $nowurl[0]);

        if ($this->privs_menu != "") {
            $privs = $this->privs_menu;
        } else {
            $privs = $this->privs_menu = RPHP::loadConfig("config/priv");
        }

        $i = 0;
        foreach ($privs['action'] as $k => $r) {

            if ($umodel[$k] || ($userpriv == "ALL" && $k != "supplier")) {

                $menu[$i]['name']    = $privs['model'][$k][0];
                $menu[$i]['display'] = $privs['model'][$k][1];
                $menu[$i]['icon']    = $privs['model'][$k][2];
                foreach ($r as $j => $s) {
                    $path = "/" . $k . "/" . $s[1];
                    if ($umodel[$k][$s[2]] || $userpriv == "ALL") {
                        $menu[$i]['list'][] = array($s[0], $path);
                    }

                    if ($k == $nowmodel[1]) {
                        $menu[$i]['active'] = 1;
                    }

                    if ($path == $nowurl[0]) {
                        $menu[$i]['curr'] = $path;
                    }

                }
                $i += 1;

            }

        }
        return $menu;
    }

    public function GotoSignin()
    {
        $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        header("location: /?code=800&ref=" . urlencode($url));
        exit;
    }

}
